<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    

    

    



    <meta charset="utf-8">
    
    
    <meta name="sogou_site_verification" content="true">
    
    
    
    <title>How to learning Shiro(2) | Lvshen&#39;s Blog | This is My World</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="Shiro,权限">
    <meta name="baidu-site-verification" content="VIVNdSiMZm">
    <meta name="description" content="前面我们学习了Shiro的基本架构，以及Shiro的认证和授权机制，今天我们聊聊Shiro在xml文件中的配置 Shiro的配置主要有如下几个部分：  对象和属性的定义与配置 URL的过滤器配置 静态用户配置 静态角色配置  其中，用户与角色一般有后台操作，其数据是动态的，所以xml的配置主要包含前两项。 我们主要讲解Spring XML的文件配置。">
<meta name="keywords" content="Shiro,权限">
<meta property="og:type" content="article">
<meta property="og:title" content="How to learning Shiro(2)">
<meta property="og:url" content="https://lvshen9.gitee.io/2017/09/01/How-to-learning-Shiro-2/index.html">
<meta property="og:site_name" content="Lvshen&#39;s Blog">
<meta property="og:description" content="前面我们学习了Shiro的基本架构，以及Shiro的认证和授权机制，今天我们聊聊Shiro在xml文件中的配置 Shiro的配置主要有如下几个部分：  对象和属性的定义与配置 URL的过滤器配置 静态用户配置 静态角色配置  其中，用户与角色一般有后台操作，其数据是动态的，所以xml的配置主要包含前两项。 我们主要讲解Spring XML的文件配置。">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2017-09-01T13:41:20.287Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="How to learning Shiro(2)">
<meta name="twitter:description" content="前面我们学习了Shiro的基本架构，以及Shiro的认证和授权机制，今天我们聊聊Shiro在xml文件中的配置 Shiro的配置主要有如下几个部分：  对象和属性的定义与配置 URL的过滤器配置 静态用户配置 静态角色配置  其中，用户与角色一般有后台操作，其数据是动态的，所以xml的配置主要包含前两项。 我们主要讲解Spring XML的文件配置。">
    
    <link rel="shortcut icon" href="/img/mylogo.jpg">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">我的技术小房间</h5>
          <a href="mailto:https://lvshen9.github.io" title="https://lvshen9.github.io" class="mail">https://lvshen9.github.io</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/about"  >
                <i class="icon icon-lg icon-address-book"></i>
                About
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/collection"  >
                <i class="icon icon-lg icon-apple"></i>
                Collection
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://lvshen9.github.io/" target="_blank" >
                <i class="icon icon-lg icon-wordpress"></i>
                Blog
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/lvshen9" target="_blank" >
                <i class="icon icon-lg icon-github-alt"></i>
                GitHub
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">How to learning Shiro(2)</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">How to learning Shiro(2)</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-09-01T12:52:05.000Z" itemprop="datePublished" class="page-time">
  2017-09-01
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/技术/">技术</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Shiro对象的配置"><span class="post-toc-number">1.</span> <span class="post-toc-text">Shiro对象的配置</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Shiro过滤器的配置"><span class="post-toc-number">2.</span> <span class="post-toc-text">Shiro过滤器的配置</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#URL过滤器配置说明"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">URL过滤器配置说明</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#URL表达式说明"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">URL表达式说明</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#Filter-Chain定义说明"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">Filter Chain定义说明</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Shiro与Spring整合"><span class="post-toc-number">3.</span> <span class="post-toc-text">Shiro与Spring整合</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#在web-xml中添加shiro过滤器"><span class="post-toc-number">3.1.</span> <span class="post-toc-text">在web.xml中添加shiro过滤器</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#在Spring的applicationContext-xml中添加shiro配置"><span class="post-toc-number">3.2.</span> <span class="post-toc-text">在Spring的applicationContext.xml中添加shiro配置</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#实现MyRealm：继承AuthorizingRealm，并重写认证授权方法"><span class="post-toc-number">3.3.</span> <span class="post-toc-text">实现MyRealm：继承AuthorizingRealm，并重写认证授权方法</span></a></li></ol></li></ol>
        </nav>
    </aside>


<article id="post-How-to-learning-Shiro-2"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">How to learning Shiro(2)</h1>
        <div class="post-meta">
            <time class="post-time" title="2017-09-01 20:52:05" datetime="2017-09-01T12:52:05.000Z"  itemprop="datePublished">2017-09-01</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/技术/">技术</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>前面我们学习了Shiro的基本架构，以及Shiro的认证和授权机制，今天我们聊聊Shiro在<code>xml</code>文件中的配置</p>
<p>Shiro的配置主要有如下几个部分：</p>
<ul>
<li>对象和属性的定义与配置</li>
<li>URL的过滤器配置</li>
<li>静态用户配置</li>
<li>静态角色配置</li>
</ul>
<p>其中，用户与角色一般有后台操作，其数据是动态的，所以<code>xml</code>的配置主要包含前两项。</p>
<p>我们主要讲解Spring XML的文件配置。</p>
<a id="more"></a>
<h4 id="Shiro对象的配置"><a href="#Shiro对象的配置" class="headerlink" title="Shiro对象的配置"></a>Shiro对象的配置</h4><p>主要是对Shiro各个组件的实现进行定义配置</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"securityManager"</span> <span class="attr">class</span>=<span class="string">"org.apache.shiro.mgt.DefaultSecurityManager"</span>&gt;</span>  </span><br><span class="line">      <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"cacheManager"</span> <span class="attr">ref</span>=<span class="string">"cacheManager"</span>/&gt;</span>  </span><br><span class="line">      <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"sessionMode"</span> <span class="attr">value</span>=<span class="string">"native"</span>/&gt;</span>  </span><br><span class="line">      <span class="comment">&lt;!-- Single realm app.  If you have multiple realms, use the 'realms' property instead. --&gt;</span>  </span><br><span class="line">      <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"realm"</span> <span class="attr">ref</span>=<span class="string">"myRealm"</span>/&gt;</span>  </span><br><span class="line">      <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"sessionManager"</span> <span class="attr">ref</span>=<span class="string">"sessionManager"</span>/&gt;</span>   </span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="Shiro过滤器的配置"><a href="#Shiro过滤器的配置" class="headerlink" title="Shiro过滤器的配置"></a>Shiro过滤器的配置</h4><p>Shiro主要是通过URL(<code>粗粒度的权限控制</code>)过滤来进行安全管理，这里的配置便是指定具体授权规则定义。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"shiroFilter"</span> <span class="attr">class</span>=<span class="string">"org.apache.shiro.spring.web.ShiroFilterFactoryBean"</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"securityManager"</span> <span class="attr">ref</span>=<span class="string">"securityManager"</span>/&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"loginUrl"</span> <span class="attr">value</span>=<span class="string">"/login.jsp"</span>/&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"successUrl"</span> <span class="attr">value</span>=<span class="string">"/home.jsp"</span>/&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"unauthorizedUrl"</span> <span class="attr">value</span>=<span class="string">"/unauthorized.jsp"</span>/&gt;</span> --&gt;  </span><br><span class="line"><span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"filterChainDefinitions"</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">value</span>&gt;</span>  </span><br><span class="line">     <span class="comment">&lt;!--some example chain definitions:--&gt;</span></span><br><span class="line">        /admin/** = authc, roles[admin]  </span><br><span class="line">        /docs/** = authc, perms[document:read]  </span><br><span class="line">        /** = authc  </span><br><span class="line">     <span class="comment">&lt;!--more URL-to-FilterChain definitions here--&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">value</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">property</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h5 id="URL过滤器配置说明"><a href="#URL过滤器配置说明" class="headerlink" title="URL过滤器配置说明"></a>URL过滤器配置说明</h5><p>Shiro可以通过配置文件实现基于URL的授权验证。FilterChain定义格式：<br><code>URL_Ant_Path_Expression = Path_Specific_Filter_Chain</code><br>每个URL配置，表示匹配该URL的应用程序请求将由对应的过滤器进行验证。<br>例如： </p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[urls] </span><br><span class="line">/index.html = anon </span><br><span class="line">/user/create = anon </span><br><span class="line">/user/** = authc </span><br><span class="line">/admin/** = authc, roles[administrator] </span><br><span class="line">/rest/** = authc, rest </span><br><span class="line">/remoting/rpc/** = authc, perms["remote:invoke"]</span><br></pre></td></tr></table></figure>
<h5 id="URL表达式说明"><a href="#URL表达式说明" class="headerlink" title="URL表达式说明"></a>URL表达式说明</h5><p>1、URL目录是基于HttpServletRequest.getContextPath()此目录设置<br>2、URL可使用通配符，**代表任意子目录<br>3、Shiro验证URL时，URL匹配成功便不再继续匹配查找。所以要注意配置文件中的URL顺序，尤其在使用通配符时。 </p>
<h5 id="Filter-Chain定义说明"><a href="#Filter-Chain定义说明" class="headerlink" title="Filter Chain定义说明"></a>Filter Chain定义说明</h5><p>1、一个URL可以配置多个Filter，使用逗号分隔<br>2、当设置多个过滤器时，全部验证通过，才视为通过<br>3、部分过滤器可指定参数，如perms，roles </p>
<h4 id="Shiro与Spring整合"><a href="#Shiro与Spring整合" class="headerlink" title="Shiro与Spring整合"></a>Shiro与Spring整合</h4><h5 id="在web-xml中添加shiro过滤器"><a href="#在web-xml中添加shiro过滤器" class="headerlink" title="在web.xml中添加shiro过滤器"></a>在web.xml中添加shiro过滤器</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- Shiro filter--&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">filter</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">filter-name</span>&gt;</span>shiroFilter<span class="tag">&lt;/<span class="name">filter-name</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">filter-class</span>&gt;</span>  </span><br><span class="line">      org.springframework.web.filter.DelegatingFilterProxy  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">filter-class</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">filter</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">filter-mapping</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">filter-name</span>&gt;</span>shiroFilter<span class="tag">&lt;/<span class="name">filter-name</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/*<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">filter-mapping</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h5 id="在Spring的applicationContext-xml中添加shiro配置"><a href="#在Spring的applicationContext-xml中添加shiro配置" class="headerlink" title="在Spring的applicationContext.xml中添加shiro配置"></a>在Spring的applicationContext.xml中添加shiro配置</h5><p><strong>1、添加shiroFilter定义</strong> </p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- Shiro Filter --&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"shiroFilter"</span> <span class="attr">class</span>=<span class="string">"org.apache.shiro.spring.web.ShiroFilterFactoryBean"</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"securityManager"</span> <span class="attr">ref</span>=<span class="string">"securityManager"</span> /&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"loginUrl"</span> <span class="attr">value</span>=<span class="string">"/login"</span> /&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"successUrl"</span> <span class="attr">value</span>=<span class="string">"/user/list"</span> /&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"unauthorizedUrl"</span> <span class="attr">value</span>=<span class="string">"/login"</span> /&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"filterChainDefinitions"</span>&gt;</span>  </span><br><span class="line">      <span class="tag">&lt;<span class="name">value</span>&gt;</span>  </span><br><span class="line">          /login = anon  </span><br><span class="line">          /user/** = authc  </span><br><span class="line">          /role/edit/* = perms[role:edit]  </span><br><span class="line">          /role/save = perms[role:edit]  </span><br><span class="line">          /role/list = perms[role:view]  </span><br><span class="line">          /** = authc  </span><br><span class="line">      <span class="tag">&lt;/<span class="name">value</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;/<span class="name">property</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">   shiroFilter 中 loginUrl 为登录页面地址，</span></span><br><span class="line"><span class="comment">   successUrl 为登录成功页面地址（如果首先访问受保护 URL 登录成功，则跳转到实际访问页面），</span></span><br><span class="line"><span class="comment">   unauthorizedUrl 认证未通过访问的页面（前面提到的“未经授权页面”）。</span></span><br><span class="line"><span class="comment"> --&gt;</span></span><br></pre></td></tr></table></figure>
<p><strong>2、添加securityManager定义</strong> </p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"securityManager"</span> <span class="attr">class</span>=<span class="string">"org.apache.shiro.web.mgt.DefaultWebSecurityManager"</span>&gt;</span>  </span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"realm"</span> <span class="attr">ref</span>=<span class="string">"myRealm"</span> /&gt;</span>  </span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><strong>3、添加realm定义</strong> </p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">" myRealm"</span> <span class="attr">class</span>=<span class="string">"com...MyRealm"</span> /&gt;</span></span><br></pre></td></tr></table></figure>
<h5 id="实现MyRealm：继承AuthorizingRealm，并重写认证授权方法"><a href="#实现MyRealm：继承AuthorizingRealm，并重写认证授权方法" class="headerlink" title="实现MyRealm：继承AuthorizingRealm，并重写认证授权方法"></a>实现MyRealm：继承AuthorizingRealm，并重写认证授权方法</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyRealm</span> <span class="keyword">extends</span> <span class="title">AuthorizingRealm</span></span>&#123;  </span><br><span class="line">    <span class="keyword">private</span> AccountManager accountManager;  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAccountManager</span><span class="params">(AccountManager accountManager)</span> </span>&#123;  </span><br><span class="line">        <span class="keyword">this</span>.accountManager = accountManager;  </span><br><span class="line">    &#125;  </span><br><span class="line">    <span class="comment">/** </span></span><br><span class="line"><span class="comment">     * 授权信息 </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="function"><span class="keyword">protected</span> AuthorizationInfo <span class="title">doGetAuthorizationInfo</span><span class="params">(  </span></span></span><br><span class="line"><span class="function"><span class="params">               PrincipalCollection principals)</span> </span>&#123;  </span><br><span class="line">       String username=(String)principals.fromRealm(getName()).iterator().next();  </span><br><span class="line">       <span class="keyword">if</span>( username != <span class="keyword">null</span> )&#123;  </span><br><span class="line">          User user = accountManager.get( username );  </span><br><span class="line">          <span class="keyword">if</span>( user != <span class="keyword">null</span> &amp;&amp; user.getRoles() != <span class="keyword">null</span> )&#123;  </span><br><span class="line">             SimpleAuthorizationInfo info = <span class="keyword">new</span> SimpleAuthorizationInfo();  </span><br><span class="line">             <span class="keyword">for</span>( SecurityRole each: user.getRoles() )&#123;  </span><br><span class="line">                   info.addRole(each.getName());  </span><br><span class="line">                   info.addStringPermissions(each.getPermissionsAsString());  </span><br><span class="line">             &#125;  </span><br><span class="line">              <span class="keyword">return</span> info;  </span><br><span class="line">          &#125;  </span><br><span class="line">       &#125;  </span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;  </span><br><span class="line">    &#125;  </span><br><span class="line">      <span class="comment">/** </span></span><br><span class="line"><span class="comment">    * 认证信息 </span></span><br><span class="line"><span class="comment">     */</span>  </span><br><span class="line">    <span class="function"><span class="keyword">protected</span> AuthenticationInfo <span class="title">doGetAuthenticationInfo</span><span class="params">(  </span></span></span><br><span class="line"><span class="function"><span class="params">                AuthenticationToken authcToken )</span> <span class="keyword">throws</span> AuthenticationException </span>&#123;  </span><br><span class="line">        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;  </span><br><span class="line">        String userName = token.getUsername();  </span><br><span class="line">        <span class="keyword">if</span>( userName != <span class="keyword">null</span> &amp;&amp; !<span class="string">""</span>.equals(userName) )&#123;  </span><br><span class="line">           User user = accountManager.login(token.getUsername(),  </span><br><span class="line">                    String.valueOf(token.getPassword()));  </span><br><span class="line">           <span class="keyword">if</span>( user != <span class="keyword">null</span> )  </span><br><span class="line">             <span class="keyword">return</span> <span class="keyword">new</span> SimpleAuthenticationInfo(  </span><br><span class="line">               user.getLoginName(),user.getPassword(), getName());  </span><br><span class="line">        &#125;  </span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;  </span><br><span class="line">    &#125;  </span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-09-01T13:41:20.287Z" itemprop="dateUpdated">2017-09-01 21:41:20</time>
</span><br>


        
        原文链接：<a href="/2017/09/01/How-to-learning-Shiro-2/" target="_blank" rel="external">https://lvshen9.gitee.io/2017/09/01/How-to-learning-Shiro-2/</a>
        
    </div>
    
    <footer>
        <a href="https://lvshen9.gitee.io">
            <img src="/img/avatar.jpg" alt="我的技术小房间">
            我的技术小房间
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Shiro/">Shiro</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/权限/">权限</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lvshen9.gitee.io/2017/09/01/How-to-learning-Shiro-2/&title=《How to learning Shiro(2)》 — Lvshen's Blog&pic=https://lvshen9.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lvshen9.gitee.io/2017/09/01/How-to-learning-Shiro-2/&title=《How to learning Shiro(2)》 — Lvshen's Blog&source=前面我们学习了Shiro的基本架构，以及Shiro的认证和授权机制，今天我们聊聊Shiro在xml文件中的配置
Shiro的配置主要有如下几个部分：

对..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lvshen9.gitee.io/2017/09/01/How-to-learning-Shiro-2/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《How to learning Shiro(2)》 — Lvshen's Blog&url=https://lvshen9.gitee.io/2017/09/01/How-to-learning-Shiro-2/&via=https://lvshen9.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lvshen9.gitee.io/2017/09/01/How-to-learning-Shiro-2/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/09/02/死锁的那点事儿/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">死锁的那点事儿</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/08/31/Shiro学习/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">How to learning Shiro</h4>
      </a>
    </div>
  
</nav>



    











    <!-- Valine Comments -->
    <div class="comments vcomment" id="comments"></div>
    <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
    <script src="//unpkg.com/valine@latest/dist/Valine.min.js"></script>
    <!-- Valine Comments script -->
    <script>
        var GUEST_INFO = ['nick','mail','link'];
        var guest_info = 'nick,mail,link'.split(',').filter(function(item){
          return GUEST_INFO.indexOf(item) > -1
        });
        new Valine({
            el: '#comments',
            notify: 'false' == 'true',
            verify: 'false' == 'true',
            appId: "dy9kXHwg5jQUlLryQmpjWRlM-gzGzoHsz",
            appKey: "P9Nh39Ol0JbMMiYqNGHEP3ml",
            avatar: "mm",
            placeholder: "Just go go",
            guest_info: guest_info.length == 0 ? GUEST_INFO : guest_info,
            pageSize: "10"
        })
    </script>
    <!-- Valine Comments end -->







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="https://lvshen9.github.io/blog2/pay/weixin.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="https://lvshen9.github.io/blog2/pay/weixin.jpg" data-alipay="https://lvshen9.github.io/blog2/pay/zhifu.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
            <span>博客内容遵循 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>我的技术小房间 &copy; 2015 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lvshen9.gitee.io/2017/09/01/How-to-learning-Shiro-2/&title=《How to learning Shiro(2)》 — Lvshen's Blog&pic=https://lvshen9.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lvshen9.gitee.io/2017/09/01/How-to-learning-Shiro-2/&title=《How to learning Shiro(2)》 — Lvshen's Blog&source=前面我们学习了Shiro的基本架构，以及Shiro的认证和授权机制，今天我们聊聊Shiro在xml文件中的配置
Shiro的配置主要有如下几个部分：

对..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lvshen9.gitee.io/2017/09/01/How-to-learning-Shiro-2/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《How to learning Shiro(2)》 — Lvshen's Blog&url=https://lvshen9.gitee.io/2017/09/01/How-to-learning-Shiro-2/&via=https://lvshen9.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lvshen9.gitee.io/2017/09/01/How-to-learning-Shiro-2/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=https://lvshen9.gitee.io/2017/09/01/How-to-learning-Shiro-2/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
